package com.cwd.arm.inter.controller;

import com.cwd.arm.application.dto.ReceiveRecordDto;
import com.cwd.arm.application.service.read.facade.ReceiveRecordReadService;
import com.cwd.arm.application.service.write.facade.ReceiveRecordWriteService;
import com.cwd.arm.inter.query.ReceiveRecordQuery;
import com.cwd.arm.inter.util.DtoToVoUtil;
import com.cwd.arm.inter.util.QueryToDaoQueryUtil;
import com.cwd.arm.inter.util.VoToDtoUtil;
import com.cwd.arm.inter.vo.ResponseVo;
import com.cwd.arm.inter.vo.receiveRecord.ReceiveRecordAddVo;
import com.cwd.arm.inter.vo.receiveRecord.ReceiveRecordUpdateVo;
import com.cwd.arm.inter.vo.receiveRecord.ReceiveRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* Description:收款记录
* Created on 2021年11月16日
* @author  yan
*/
@Api(value = "收款记录接口", description = "收款记录接口")
@RestController
@RequestMapping(value = "/receiveRecord")
public class ReceiveRecordController {

    @Autowired
    private ReceiveRecordReadService readService;

    @Autowired
    private ReceiveRecordWriteService writeService;

    @ApiOperation(value = "查询收款记录信息", notes = "查询收款记录信息")
    @ApiImplicitParams(value = {
    @ApiImplicitParam(name = "id", value = "",required = true,dataType = "Long", paramType = "path")
    })
    @GetMapping("/{id}")
    public ResponseVo<ReceiveRecordVo> getReceiveRecord(@PathVariable Long id) {
        ReceiveRecordDto dto = readService.selectById(id);
        return ResponseVo.<ReceiveRecordVo>BUILDER().success(convertToVo(dto));
    }

    @ApiOperation(value = "查询收款记录列表", notes = "查询收款记录列表")
    @GetMapping("")
    public ResponseVo<List<ReceiveRecordVo>> getReceiveRecordList(ReceiveRecordQuery query) {
        List<ReceiveRecordDto> dtoList = readService.selectByQuery(QueryToDaoQueryUtil.toDaoQuery(query));
        return ResponseVo.<List<ReceiveRecordVo>>BUILDER().success(DtoToVoUtil.convertToReceiveRecordVoList(dtoList));
    }

    @ApiOperation(value = "新增收款记录", notes = "新增收款记录")
    @Secured({"ROLE_RECORD_ADD"})
    @PostMapping("")
    public ResponseVo<String> add(@RequestBody ReceiveRecordAddVo addVo) {
        writeService.save(VoToDtoUtil.convertToDto(addVo));
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "修改收款记录", notes = "修改收款记录")
    @Secured({"ROLE_RECORD_EDIT"})
    @PutMapping("")
    public ResponseVo<String> update(@RequestBody ReceiveRecordUpdateVo updateVo) {
        writeService.update(VoToDtoUtil.convertToDto(updateVo));
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "删除收款记录", notes = "删除收款记录")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "收款记录id",required = true,dataType = "Long", paramType = "path")
    })
    @Secured({"ROLE_RECORD_DEL"})
    @DeleteMapping("/{id}")
    public ResponseVo<String> del(@PathVariable Long id) {
        writeService.del(id);
        return ResponseVo.<String>BUILDER().success();
    }


    private ReceiveRecordVo convertToVo(ReceiveRecordDto dto){
        ReceiveRecordVo vo = new ReceiveRecordVo();
        BeanUtils.copyProperties(dto,vo);
        return vo;
    }

}
